﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.WeApp;
using BrnMall.Manager.Filter;
using PetaPoco;

namespace BrnMall.Manager.Manager.WeApp
{
    /// <summary>
    /// 用户小程序
    /// </summary>
    public static class StoreApplicationManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static StoreApplication GetStoreApplication(int storeAppId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<StoreApplication>(storeAppId);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="authId"></param>
        /// <returns></returns>

        public static StoreApplication GetStoreApplicationByAuthId(int authId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<StoreApplication>("select * from weapp_storeapp where authorizationId=@0", authId);
            }
        }
        /// <summary>
        /// 解除授权
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteAuthorization(int id)
        {
            using (var db = new QDMallDatabase())
            {
                try
                {
                    var authorizationId = db.ExecuteScalar<int>("select authorizationId from weapp_storeapp where storeappid=@0", id);
                    db.BeginTransaction();
                    db.Execute("update weapp_storeapp set authorizationId=0 where storeappid=@0", id);
                    db.Execute("delete from weapp_authorization where id=@0", authorizationId);
                    db.CompleteTransaction();
                }
                catch
                {
                    db.AbortTransaction();
                    throw;
                }


            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        public static void AddStoreApplication(StoreApplication app)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(app);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        public static void UpdateStoreApplication(StoreApplication app)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(app);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteStoreApplication(int id)
        {
            DeleteAuthorization(id);
            using (var db = new QDMallDatabase())
            {
                db.Delete<StoreApplication>(id);
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public static IList<StoreApplication> GetStoreApplications(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StoreApplication>("select * from weapp_storeapp where storeid=@0", storeId);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="page"></param>
        /// <param name="itemsPerPage"></param>
        /// <returns></returns>
        public static Page<StoreApplication> GetStoreApplicationsPage(StoreApplicatioinFilter condition, long page = 1, long itemsPerPage = 15)
        {
            using (var db = new QDMallDatabase())
            {
                var sql=new StringBuilder(50);
                sql.Append("select * from weapp_storeapp where 1=1");
                if (condition != null)
                {
                    if (condition.StoreAppId > 0)
                        sql.AppendFormat(" and StoreAppId={0}", condition.StoreAppId);
                    if (condition.StoreId > 0)
                        sql.AppendFormat(" and storeid={0}", condition.StoreId);
                    if (condition.AppId > 0)
                        sql.AppendFormat(" and appid={0}", condition.AppId);
                    if (condition.DevStatus > -1)
                        sql.AppendFormat(" and devstatus={0}", condition.DevStatus);
                    if (condition.NeedUpdate > -1)
                        sql.AppendFormat(" and needupdate={0}", condition.NeedUpdate);
                }
                
                sql.Append(" order by storeappid desc");
                return db.Page<StoreApplication>(page, itemsPerPage, sql.ToString());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="devStatus"></param>
        /// <returns></returns>
        public static IList<StoreApplication> GetStoreApplicationsByStatus(int devStatus)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StoreApplication>("select * from weapp_storeapp where DevStatus=@0", devStatus);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        public static IList<StoreApplication> GetStoreApplicationsByAppId(int appId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StoreApplication>("select * from weapp_storeapp where appid=@0", appId);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wechatAppId"></param>
        /// <returns></returns>
        public static StoreApplication GetStoreApplicationsByWeChatAppId(string wechatAppId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<StoreApplication>("select a.* from weapp_storeapp a left join weapp_authorization b on a.authorizationId=b.id where b.authorizer_appid=@0 ", wechatAppId);
            }
        }
        /// <summary>
        /// 更新开发的状态开发状态0:未知，1:待上传，2:已上传，3：待审核 4:审核结束，5:已发布
        /// </summary>
        /// <param name="storeAppId"></param>
        /// <param name="devStatus">开发状态0:未知，1:待上传，2:已上传，3：待审核 4:审核结束，5:已发布</param>
        public static void UpdateDevStatus(int storeAppId, int devStatus)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_storeapp set DevStatus=@0 where storeappid=@1", devStatus, storeAppId);
                if (devStatus == 2)
                    db.Execute("update weapp_storeapp set AuditInfo='',uploadtime=getdate() where storeappid=@0",  storeAppId);
                else if (devStatus == 3)
                    db.Execute("update weapp_storeapp set committime=getdate() where storeappid=@0", storeAppId);
            }
        }
        /// <summary>
        /// 更新错误提示
        /// </summary>
        /// <param name="storeAppId"></param>
        /// <param name="auditInfo">错误提示</param>
        public static void UpdateAuditInfo(int storeAppId, string auditInfo)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_storeapp set AuditInfo=@0 where storeappid=@1", auditInfo, storeAppId);
            }
        }
        /// <summary>
        /// 更新模板编号
        /// </summary>
        /// <param name="storeAppId"></param>
        /// <param name="templateId"></param>
        public static void UpdateTemplateId(int storeAppId, int templateId)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_storeapp set templateid=@0 where storeappid=@1", templateId, storeAppId);

            }
        }
        /// <summary>
        /// 发布上线
        /// </summary>
        /// <param name="storeAppId"></param>
        public static void ReleaseApp(int storeAppId)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_storeapp set DevStatus=5,ReleaseTime=getdate(),AuditInfo='' where storeappid=@0", storeAppId);
            }
        }
        /// <summary>
        /// 更新开发的状态
        /// </summary>
        /// <param name="storeAppId"></param>
        /// <param name="auditId"></param>
        public static void UpdateAuditId(int storeAppId, long auditId)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_storeapp set AuditId=@0 where storeappid=@1", auditId, storeAppId);
            }
        }
        #region 支付
        /// <summary>
        /// 
        /// </summary>
        /// <param name="storeAppId"></param>
        /// <returns></returns>
        public static StoreApplicationPaySetting GetStoreApplicationPaySetting(int storeAppId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<StoreApplicationPaySetting>(
                    "select top 1 * from weapp_StoreAppPaySetting where storeappid=@0", storeAppId);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        public static void AddStoreApplicationPaySetting(StoreApplicationPaySetting setting)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(setting);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        public static void UpdateStoreApplicationPaySetting(StoreApplicationPaySetting setting)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(setting);
            }
        }


        #endregion
    }
}
